package com.hit.aircraftwar.bullet;

import com.hit.aircraftwar.aircraft.AbstractAircraft;

public class TrackBullet extends AbstractBullet {

    AbstractAircraft aimedAircraft = null;

    public TrackBullet(int locationX, int locationY, int speedX, int speedY, int power) {
        super(locationX, locationY, speedX, speedY, power);
    }

    public void aiming(AbstractAircraft aircraft) {
        if(this.aimedAircraft == null || this.aimedAircraft.notValid()) {
            this.aimedAircraft = aircraft;
        }
    }

    @Override
    public void forward() {
        if (aimedAircraft != null && !aimedAircraft.notValid()) {
            int x = aimedAircraft.getLocationX() - this.locationX;
            int y = aimedAircraft.getLocationY() - this.locationY;
            double speedFix = Math.sqrt(x * x + y * y);
            if (speedFix < 1) {
                this.speedY = -24;
                this.speedX = 0;
            } else {
                this.speedY = (int) (y / speedFix * 50);
                this.speedX = (int) (x / speedFix * 50);
            }
        }
        super.forward();
    }

    @Override
    public void bombed() {
    }
}
